let child_process = require('node:child_process')
let {resolve} = require('node:path')

let elasticsearchBinPath = resolve('../../cache/elasticsearch-8.12.0/bin/');

let elasticsearchCommand = resolve(elasticsearchBinPath, 'elasticsearch' + (process.platform === 'win32' ? '.bat' : ''));
console.log('command', elasticsearchCommand)

const elasticsearchProcess = child_process.spawn(
    elasticsearchCommand,
    [],
    {
      detached: true,
      windowsHide: true,
    },
);
elasticsearchProcess.on('data', args => {
  console.log(`执行事件 data: ${args}`);
});
elasticsearchProcess.on('exit', (code, signal) => {
  console.log(`执行事件 exit, code: ${code}, signal: ${signal}`);
});
elasticsearchProcess.on('spawn', () => {
  console.log(`执行事件 spawn`);
});
elasticsearchProcess.on('disconnect', () => {
  console.log(`执行事件 disconnect`);
});
elasticsearchProcess.on('message', message => {
  console.log(`执行事件 message: ${message}`);
});
elasticsearchProcess.on('close', () => {
  console.log(`执行事件 close`);
  process.nextTick(function () {
    process.exit(0);
  });
});
elasticsearchProcess.on('error', (error) => {
  console.error(`执行事件 error: ${error}`);
});

// // 初始化密码
// const setPasswordsCommand = resolve(elasticsearchBinPath, 'elasticsearch-setup-passwords');
// const setPasswordsProcess = child_process.spawnSync(
//     setPasswordsCommand,
//     ['auto'],
//     {
//       stdio: 'inherit',
//     },
// );
//
// if (setPasswordsProcess.error) {
//   console.error('Error while setting up passwords:', setPasswordsProcess.error);
// } else {
//   console.log('Passwords setup completed successfully');
// }

// child_process.spawnSync(
//     resolve(elasticsearchBinPath, 'elasticsearch-setup-passwords'),
//     ['--batch','--reset','--password-file']
// )
